package com.zts.modules.sys.controller;

/**
 * Created by jisd on 2017/6/16.
 */

import com.zts.base.BasicResourceAssembler;
import com.zts.base.page.PageBound;
import com.zts.base.resource.ListResource;
import com.zts.modules.sys.entity.Resource;
import com.zts.modules.sys.entity.model.ResourceModel;
import com.zts.modules.sys.entity.resource.ResourceResource;
import com.zts.modules.sys.entity.resource.RoleResource;
import com.zts.modules.sys.service.ResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.ExposesResourceFor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

@RestController("resourceController")
@RequestMapping({"/v1/resources"})
@Api(value = "resources", description = "资源相关API")
@ExposesResourceFor(ResourceResource.class)
public class ResourceController {
    private static Logger logger = LoggerFactory.getLogger(ResourceController.class);
    @Autowired
    ResourceService resourceService;
    private BasicResourceAssembler<Resource, ResourceResource> resourceAssembler =
            new BasicResourceAssembler(ResourceController.class, ResourceResource.class);


    /**
     * 查询全部系统资源
     *
     * @param resourceModel
     * @param pageBound
     * @return
     */
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    @RequestMapping(value = "", method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(notes = "getResources", httpMethod = "GET", value = "获取资源的信息")
    public final ListResource<ResourceResource> getResources(@ApiParam(required = false, value = "资源的查询条件")
                                                             @ModelAttribute("ResourceModel") ResourceModel resourceModel, PageBound pageBound) {
        List list = resourceService.getList(resourceModel.transEntity(), pageBound);
        return resourceAssembler.toListResource(list);
    }

    /**
     * 根据主键查询某个资源
     *
     * @param id
     * @return
     */
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    @RequestMapping(value = "/{id}", method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(notes = "getResourceById", httpMethod = "GET", value = "根据主键id查询资源")
    public final ResourceResource getResource(@ApiParam(required = true, value = "资源主键id") @PathVariable("id") Long id) {
        Resource resource = resourceService.get(id);
        return resourceAssembler.toResource(resource);
    }


    /**
     * 添加特定资源
     *
     * @param resourceModel
     * @return
     */
    @ResponseBody
    @ResponseStatus(HttpStatus.CREATED)
    @RequestMapping(value = {""}, method = {RequestMethod.POST},
            consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_VALUE},
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(notes = "addResource", httpMethod = "POST", value = "新增资源")
    public final ResourceResource insertResource(@ApiParam(required = true, value = "资源信息，接收数据格式为json") @RequestBody ResourceModel resourceModel) {
        Resource resource = resourceService.insert(resourceModel.transEntity());
        return resourceAssembler.toResource(resource);
    }


    /**
     * 根据主键Id更新特定资源
     *
     * @param id
     * @param resourceModel
     * @return
     */
    @ResponseBody
    @ResponseStatus(HttpStatus.CREATED)
    @RequestMapping(value = {"/{id}"}, method = {RequestMethod.PUT},
            consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_VALUE},
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(notes = "updateResource", httpMethod = "PUT", value = "根据主键id修改资源信息")
    public final int updateResource(@ApiParam(required = true, value = "资源主键id") @PathVariable("id") Long id,
                                    @ApiParam(required = true, value = "要修改的用户信息项及对应修改的值，接收数据格式为json")
                                    @RequestBody ResourceModel resourceModel) {
        logger.debug("updateResource param：{}", resourceModel.toString());
        resourceModel.setId(id);
        return resourceService.update(resourceModel.transEntity());
    }


    /**
     * 根据id删除特定资源
     *
     * @param id
     * @return
     */
    @ResponseBody
    @ResponseStatus(HttpStatus.NO_CONTENT)
    @RequestMapping(value = {"/{id}"}, method = {RequestMethod.DELETE}, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(notes = "deleteResource", httpMethod = "DELETE", value = "根据主键id删除资源")
    public final int deleteResource(@ApiParam(required = true, value = "资源主键id") @PathVariable("id") Long id) {
        Resource resource = new Resource();
        resource.setId(id);
        logger.info("id=====" + id);
        return resourceService.delete(resource);
    }

    //========================================资源与角色 begin============================================
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    @RequestMapping(value = "/{id}/roles", method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(notes = "getRoles", httpMethod = "GET", value = "查询拥有当前资源的角色")
    public final ListResource<RoleResource> getRoles(@ApiParam(value = "资源标识id", required = true)
                                                     @PathVariable("id") Long id) {
        return null;
    }

    @ResponseBody
    @ResponseStatus(HttpStatus.CREATED)
    @RequestMapping(value = "/roles", method = RequestMethod.POST,
            consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE},
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(notes = "insertRoleResource", httpMethod = "POST", value = "将资源分配给角色")
    public final int insertRoleResource(@ApiParam(value = "资源标识id", required = true)
                                        @RequestParam("resourceIds") List<Long> resourceIds,
                                        @ApiParam(value = "角色标识id", required = true)
                                        @RequestParam("roleIds") List<Long> roleIds) {
        return 0;
    }


    @ResponseBody
    @ResponseStatus(HttpStatus.NO_CONTENT)
    @RequestMapping(value = "/roles", method = RequestMethod.DELETE,
            consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE},
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(notes = "deleteRoleResource", httpMethod = "DELETE", value = "删除角色拥有的资源")
    public final int deleteRoleResource(@ApiParam(value = "资源标识id", required = true)
                                        @RequestParam("resourceIds") List<Long> resourceIds,
                                        @ApiParam(value = "角色标识id", required = true) @RequestParam("roleIds") List<Long> roleIds) {
        return 0;
    }


    //========================================资源与角色 end============================================
}